﻿<?php #Редактор таблиц
class mytable_editor extends Load_use
{
	function getBlock()
	{
		GLOBAL $conn, $query;
		$table_name 					= $this->params['table_name'];
		$main_column					= $this->params['main_column'];
		$order_by					    = isset($this->params['order_by']) ? $this->params['order_by'] : "";
        $del_button					    = ( isset($this->params['editable']) && $this->params['editable'] == true ) ? '<span class="del_row"></span>' : '';

		$mytable_editor_html 			= $mytable_editor_head = $mytable_editor_body = $example_string = $add_new_strings = '';
		$mytable_editor_column_names 	= array();
		
		if( $table_name == 'vip_service_category' ) {
			$conn->query("SELECT distinct id, parents, title, templ FROM pages WHERE templ IN('category','sub_category', 'product') order by parents");
			
			$structure = array();
			
			while( $row = $conn->fetch() ) {
				if( $row->templ == 'category' ) {
					$structure['#'.$row->id.'#'] = array();
					continue; 
				}
				if( $row->templ == 'sub_category' ) {
					if( preg_match('/(#\d+#){1}/', $row->parents, $separate_parent) ) {
					
						$structure[$separate_parent[0]][$separate_parent[0].'#'.$row->id.'#'] = array();
						
					}
					if( preg_match('/(#\d+#){2}/', $row->parents, $separate_parent) ) {
						preg_match('/(#\d+#){1}/', $row->parents, $separate_parent1);
						
						$structure[$separate_parent1[0]][$separate_parent[0]][$separate_parent[0].'#'.$row->id.'#'] = array();
					}
				}
				if( $row->templ == 'product' ) {
					if( preg_match('/(#\d+#){2}/', $row->parents, $separate_parent2) ) {
						preg_match('/(#\d+#){1}/', $row->parents, $separate_parent1);
						
						$structure[$separate_parent1[0]][$separate_parent2[0]][$row->title] = $separate_parent2[0].'#'.$row->id.'#';
					}
					if( preg_match('/(#\d+#){3}/', $row->parents, $separate_parent3) ) {
						preg_match('/(#\d+#){1}/', $row->parents, $separate_parent1);
						preg_match('/(#\d+#){2}/', $row->parents, $separate_parent2);
						
						$structure[$separate_parent1[0]][$separate_parent2[0]][$separate_parent3[0]][$row->title] = $separate_parent3[0].'#'.$row->id.'#';
					}
				}
			}
			
			/* echo '<pre>';
			print_r($structure);
			echo '</pre>'; */
		}

		$conn->query("SELECT * FROM {$table_name} {$order_by}",false,'all_from_table',$resource);
		$conn->query("SHOW COLUMNS FROM {$table_name}", false, 'columns_num');
		$column_count = $conn->rowCount('columns_num');
		
		for ($i = 0; $i < $column_count; $i++) {
			$mytable_editor_column_names[] = mysql_field_name($resource, $i);
		}
		
		$example_string = '
		<table>
			<tr class="new_string_example">';
				foreach( $mytable_editor_column_names as $column_name ) {
					$mytable_editor_head .= '<th>'.$column_name.'</th>';
					$example_string .= '<td><input name="'.$column_name.'" placeholder="'.$column_name.'"></td>';
				}

				$example_string .= '
				<td class="edit_cancel_row">
					<div class="td_buttons">
						<span class="cancel_row"></span>
					</div>
				</td>
			</tr>
		</table>';
		
		while( $row = $conn->fetch('all_from_table') ){
			$mytable_editor_body .= '<tr>';
			
			for ($i = 0; $i < $column_count; $i++) {
				$mytable_editor_body .= '
					<td class="'.$mytable_editor_column_names[$i].'">'.$row->$mytable_editor_column_names[$i].'</td>
				';
			}
			
			$mytable_editor_body .= '
				<td class="edit_cancel_row">
					<div class="td_buttons">
						<span class="edit"></span>
						'.$del_button.'
					</div>
				</td>
			</tr>';
		}
		
		$mytable_editor_html .= '
		<p>Редактируется по <span class="main_column">'.$main_column.'</span>
		<div class="my_table_editor">
			<form name="'.$table_name.'" class="edit_rows">
				<table>
					<thead>
						<tr class="head">
							'.$mytable_editor_head.'
							<th class="edit_cancel_row">
								<div style="width: 100px;"></div>	
							</th>
						</tr>
					</thead>
					<tbody>
						'.$mytable_editor_body.'
					</tbody>
				</table>
			</form>
			
			<table>
			'.$example_string.'
			</table>
			
			<form name="'.$table_name.'" class="add_new_rows">
				<table>
				</table>
			</form>
			
			
			<div class="my_buttons_wrap">
				<span class="add_new_row">Добавить</span>
				<span class="send_table">Отправить</span>
			</div>
			<div id="results"></div>
		</div>';
		
		return $mytable_editor_html;
	}
}